package lion.dev.encrypt;

import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

/**
 * @author lion
 * @mail hl_0074@sina.com
 * @date 2011-9-26
 * @des 1,generateKey 生成一个密钥,反回base64编码字符串 2,encrypt 跟据密钥编码字符串生成密钥，跟据密钥加密数据
 *      3,decrypt 跟据密钥编码字符串生成密钥，跟据密钥解密数据
 */
public class DES {

	public static final String ALGORITHM_DES = "DES";
	public static final String ALGORITHM_TRIPLEDES = "TripleDES";
	public static final String ALGORITHM_AES = "AES";
	public static final String ALGORITHM_BLOWFISH = "Blowfish";
	public static final String ALGORITHM_RC2 = "RC2";

	private String algorithm;

	public DES(String algorithm) {

		this.algorithm = algorithm;
	}

	/**
	 * 加密byte[]数据
	 */
	public byte[] encrypt(byte[] data, String password) {

		return doFinal(data, generateKey(password), Cipher.ENCRYPT_MODE);
	}

	/**
	 * 加密字符串
	 */
	public byte[] encrypt(String data, String password) {

		return encrypt(data.getBytes(Charsets.UTF_8), password);
	}

	/**
	 * 解密，反回字节数组
	 */
	public byte[] decrypt(byte[] data, String password) {

		return doFinal(data, generateKey(password), Cipher.DECRYPT_MODE);
	}

	/**
	 * 解密，返回字符串
	 */
	public String decryptString(byte[] data, String password) {

		return new String(decrypt(data, password), Charsets.UTF_8);
	}

	/**
	 * 处理加解密
	 */
	private byte[] doFinal(byte[] data, Key key, int mode) {

		Cipher cipher;
		byte[] result = null;
		try {
			cipher = Cipher.getInstance(algorithm);
			cipher.init(mode, key);
			result = cipher.doFinal(data);
		} catch (Exception e) {
			result = new byte[0];
		}
		return result;
	}

	/**
	 * 
	 */
	private Key generateKey(String seed) {

		KeyGenerator keyGenerator = null;
		try {
			keyGenerator = KeyGenerator.getInstance(algorithm);
		} catch (Exception e) {
			throw new IllegalArgumentException("", e);
		}

		SecureRandom sr = null;
		if (StringUtils.isNotBlank(seed)) {
			sr = new SecureRandom(seed.getBytes(Charsets.UTF_8));
		} else {
			sr = new SecureRandom();
		}
		keyGenerator.init(sr);

		return keyGenerator.generateKey();
	}

	/**
	 * 解析密钥
	 */

	// private Key parseKey(String key) {
	//
	// SecretKey secretKey = null;
	// if (DES.ALGORITHM_DES.equals(algorithm)) {
	// try {
	// DESKeySpec dks = new DESKeySpec(Base64.decodeBase64(key));
	// SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
	// secretKey = skf.generateSecret(dks);
	// } catch (Exception e) {
	// }
	// } else {
	// secretKey = new SecretKeySpec(Base64.decodeBase64(key), algorithm);
	// }
	//
	// return secretKey;
	// }

	public static void main(String[] args) throws UnsupportedEncodingException {

		DES des = new DES(DES.ALGORITHM_DES);
		String password = "hl_0074@sina.com";
		System.out.println(Arrays.toString(des.generateKey(password).getEncoded()));
		String content = "中华人民共和国";
		String encrypted = Base64.encodeBase64String(des.encrypt(content.getBytes("UTF-8"), password));
		System.out.println("加密后：" + encrypted);
		System.out.println("解密后：" + new String(des.decrypt(Base64.decodeBase64(encrypted), password)));
	}
}
